<?php

use yii\helpers\Html;
use yii\widgets\LinkPager;
use yii\grid\GridView;
use app\common\Functions;

/**
 * @var yii\web\View $this
 * @var amnah\yii2\user\Module $module
 * @var array $actions
 */
global $regionTypes2;
global $prodAttrCodes;
$regionTypes2 = $regionTypes;
$module = $this->context->module;

$this->title = Yii::t('user', 'Yii 2 User');
?>
<div class="user-default-index">




    <?php ?>
    <h3><?php echo $prodSpecName . '管理'; ?></h3>
    <div class="row">
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">全部订单</span>
                    <span class="info-box-number"><?php echo $dataProvider->getTotalCount(); ?></span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">累积处理订单</span>
                    <span class="info-box-number">93,139</span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">覆盖区域</span>
                    <span class="info-box-number"> <?php echo $regionCnt . '个'; ?>   </span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
        <div class="col-md-3">
            <div class="info-box">
                <!-- Apply any bg-* class to to the icon to color it -->
                <span class="info-box-icon bg-red"><i class="fa fa-star-o"></i></span>
                <div class="info-box-content">
                    <span class="info-box-text">本日新增订单</span>
                    <span class="info-box-number"><?php echo $regionTodayCnt . '个'; ?> </span>
                </div><!-- /.info-box-content -->
            </div><!-- /.info-box -->
        </div>
    </div>
    <?php ?>
    <p>
        <?php
            $createUrl                     = [];
            $createUrl[]                   = 'create';
            $createUrl['attr_type_spec_id']   = $attr_type_spec_id;
            $createUrl['prod_spec_name'] = $prodSpecName;


            //创建区域逻辑
            // 如果创建
            echo Html::a( Html::tag('span', Yii::t('region', 'Create') .  $prodSpecName , ['class' => "glyphicon fa fa-plus-square"])   . $region_name. Yii::t('region', $region_type_name) , $createUrl, ['class' => 'btn btn-success']);
        ?>

    </p>

    <?php

        $prodAttrCodes=[];

       // Yii::trace("=====================              ============");
        //Yii::trace($dataProvider);


        // 增加展示的字段
        $columns = [];
        $i = 0;

        // 复选框
        $columns[$i]['class'] = 'yii\grid\CheckboxColumn';
        $columns[$i]['multiple'] = true;
        $columns[$i]['name'] = 'uid';
        $columns[$i]['options'] = ['style'=>'width:30px'];

        $i++;
        // id
        $columns[$i]['attribute'] = 'id';
        $columns[$i]['options'] = ['style'=>'width:30px'];

        $i++;
        //订单类型
        $columns[$i]['label'] = '订单类型名称';
        $columns[$i]['attribute'] = 'order_type_id';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'orderTypeName', ['class'=>'form-control']);
        $columns[$i]['value']   = function($model)
        {

            return $model->orderTypes->type_name;

        };



        //对象类型
        $i++;
        $columns[$i]['label'] = '订单主体';
        $columns[$i]['attribute'] = 'object_type_id';
        $columns[$i]['format'] = 'raw';
        $columns[$i]['options'] = ['style'=>'width:120px'];
        $columns[$i]['value']   = function($model) use ($objectTypes)
        {

            // 如果是客户
            if ($model->object_type_id==1)
            {

                return Html::a( $objectTypes[$model->object_type_id] . ' : ' . $model->custs->cust_name ,
                    ['/prod/cust/view', 'id'=>$model->object_id,'attr_type_spec_id'=>20001,   'prod_spec_name'=> '客户' ],
                    ['title' => "订单主体"]);

                //return $objectTypes[$model->object_type_id] . ' : ' . $model->custs->cust_name;
            }

            return Html::a( $objectTypes[$model->object_type_id] . ' : '  . $model->parentProdInsts->prod_name . ' '   .$model->prodInsts->prod_name,
                ['/prod/prodinst/view', 'id'=>$model->object_id,'prod_spec_id'=>$model->prodInsts->prod_spec_id,   'prod_spec_name'=> '楼盘' ],
                ['title' => "订单主体"]);

            //return $objectTypes[$model->object_type_id] . ' : '  . $model->parentProdInsts->prod_name . ' '   .$model->prodInsts->prod_name;


        };

        $i++;
        //订单类型
        $columns[$i]['label'] = '备注';
        $columns[$i]['attribute'] = 'remark';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'remark', ['class'=>'form-control']);


//
//        //对象类型
//        $i++;
//        $columns[$i]['label'] = '名称';
//        $columns[$i]['attribute'] = 'object_id';
//        $columns[$i]['format'] = 'raw';
//        $columns[$i]['options'] = ['style'=>'width:90px'];
//        $columns[$i]['value']   = function($model)
//        {
//            // 如果是客户
//            if ($model->object_type_id)
//            {
//                return $model->custs->cust_name;
//            }
//            return $model->prodInsts->prod_name;
//
//        };



        $i++;
        //经办人
        $columns[$i]['label'] = '经办人';
        $columns[$i]['attribute'] = 'staff_id';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'user_name', ['class'=>'form-control']);
        $columns[$i]['content']   = function($model)
        {
            return $model->dealUsers->username  ;
        };

    


        foreach ($attrSpecs as $attrSpec)
        {
            $prodAttr = $attrSpec->attrSpecs;

            $i++;
            $columns[$i]['label'] = $prodAttr->attr_spec_name;
            $prodAttrCodes[$i] = $prodAttr->attr_spec_id;
            $a = $prodAttr->attr_spec_id;
            $attr_spec_type_id = $prodAttr->attr_spec_type_id;

            $columns[$i]['value']   = function($model, $key, $index, $column)use ($a, $attr_spec_type_id)
            {
                if ($model->externalAttrs)
                {
                    foreach ($model->externalAttrs  as $attrInst)
                    {
                        if ($a == $attrInst->attr_spec_id)
                        {
                            if ($attr_spec_type_id==3)
                            {
                                return Functions::getDomainsName('attr_spec',$a,  $attrInst->content);
                            }

                            return $attrInst->content;
                        }
                    }
                }

                return  '未设置具体的值！';

            };


            $columns[$i]['options'] = ['style'=>'width:180px'];

        }

        $i++;
        //名称
        $columns[$i]['label'] = '创建人';
        $columns[$i]['attribute'] = 'create_staff_id';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['filter'] = Html::activeTextInput($searchModel, 'user_name', ['class'=>'form-control']);
        $columns[$i]['content']   = function($model)
        {
            return $model->users->username  ;
        };


        $i++;
        //名称
        $columns[$i]['label'] = '更新时间';
        $columns[$i]['attribute'] = 'modify_date';
        $columns[$i]['options'] = ['style'=>'width:180px'];

        $i++;
        //操作选项
        $columns[$i]['class']   = 'app\common\grid\ActionColumn';
        $columns[$i]['options'] = ['style'=>'width:180px'];
        $columns[$i]['buttons'] =  [
            'view' => function ($url, $model, $key) use ($prodSpecName) {
                return Html::a(Html::tag('span', '查看', ['class' => "glyphicon fa fa-eye"]), ['view', 'id'=>$model->id,   'attr_type_spec_id'=> $model->object_type_id,    'prod_spec_name' => $prodSpecName], ['class' => "btn btn-xs btn-success",'label'=>'查看子产品', 'title' => '查看子产品']);
            },

            'update' => function ($url, $model, $key) use ($prodSpecName) {
                return Html::a(Html::tag('span', '编辑', ['class' => "glyphicon fa fa-edit"]), ['update', 'id'=>$model->id,   'attr_type_spec_id'=> $model->object_type_id,    'prod_spec_name' => $prodSpecName ], ['class' => "btn btn-xs btn-success",'label'=>'查看子产品', 'title' => '查看子产品']);
            },

            'delete' => function ($url, $model, $key)  {
                return Html::a(Html::tag('span', '删除', ['class' => "glyphicon fa fa-trash"]), ['delete', 'id'=>$model->id,   'attr_type_spec_id'=> $model->object_type_id ], ['class' => "btn btn-xs btn-danger",'label'=>'删除产品', 'title' => '删除子产品']);
            },

        ];


        echo GridView::widget([
        'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
        'layout' => "{items}{summary}{pager}",
        'options'=>['id'=>'grid'],
        'columns' => $columns

        ]);


    ?>


</div>
